<?

class PostLib{
	
	//Lay Noi dung niem tin cua 1 post
	static function getContentBelive($post) {
		if($post['owner']) {
			return '<a href="'.FB_BASE_URL.'profile.php?id='.$post['fbid'].'" target="_blank">'. $post['fbname'] . '</a> ' . $post['content'];
		}
		else {
			return $post['content'];
		}
	}
	
	static function getContentBeliveForPost($post) {
		if($post['owner']) {
			return $post['fbname'] . ' ' . $post['content'];
		}
		else {
			return $post['content'];
		}
	}
	
	//Lay noi dung nhung nguoi tin
	static function getOtherBelive($post, $fbmeid = 0, $fbposterid = 1, $max_name = 4) {
		if($post['num_belive'] == 1) $content = 'tin rằng:';
		else {
			$content = ' cùng ';
			$belivers = unserialize($post['belivers']);
			unset($belivers[$fbmeid]);
			if(count($belivers) < $max_name) {
				$last_key = end(array_keys($belivers));
				foreach($belivers as $k => $v){
					$content .= ' <a href="'.FB_BASE_URL."profile.php?id=".$k.'">'.$v.'</a>';
					if($k != $last_key) {
						$content .= ',';
					}
				}
			}
			else {
				$i = 0;
				foreach($belivers as $k => $v) {
					$i++;
					if($i < $max_name) {
						$content .= ' <a href="'.FB_BASE_URL."profile.php?id=".$k.'">'.$v.'</a>';
						if($i != ($max_name - 1)) {
							$content .= ',';
						}
					}
					else {
						break;
					}
				}
				$content .= ' và <a href="javascript:void(0)" onclick="emmua.postBox.viewOtherBelive('.$post['id'].', '.$fbmeid.', '.$max_name.')">'. (count($belivers)-($max_name-1)).' người khác</a>';
			}
			$content .= ' tin rằng:';
		}
		return $content;
	}
	
	static function getOtherBeliveImage($post, $fbmeid = 0, $fbposterid = 1, $max_name = 4) {
		if($post['num_belive'] > 1) {
			$content = '';
			$belivers = unserialize($post['belivers']);
			unset($belivers[$fbmeid]);
			if(count($belivers) < $max_name) {
				$last_key = end(array_keys($belivers));
				foreach($belivers as $k => $v){
					$src = 'http://graph.facebook.com/'.$k.'/picture';
					$content .= '<a href="'.FB_BASE_URL."profile.php?id=".$k.'" title="'.$v.'"><img src="'.$src.'" width="32px"/></a>&nbsp;';
				}
			}
			else {
				foreach($belivers as $k => $v) {
					if($i < $max_name) {
						$src = 'http://graph.facebook.com/'.$k.'/picture';
						$content .= ' <a href="'.FB_BASE_URL."profile.php?id=".$k.'" title="'.$v.'"><img src="'.$src.'" width="32px"/></a>&nbsp;';
					}
					else {
						break;
					}
				}
			}
			return $content;
		}
	}
	
	//Lay noi dung nhung nguoi tin cho việc post trên Facebook
	static function getOtherBeliveForPost($post, $fbmeid = 0, $fbposterid = 1, $max_name = 4) {
		if($post['num_belive'] == 1) $content = 'tin rằng:';
		else {
			$content = ' cùng ';
			$belivers = unserialize($post['belivers']);
			unset($belivers[$fbmeid]);
			if(count($belivers) < $max_name) {
				$last_key = end(array_keys($belivers));
				foreach($belivers as $k => $v){
					$content .= ' @:'.$k;
					if($k != $last_key) {
						$content .= ',';
					}
				}
			}
			else {
				$i = 0;
				foreach($belivers as $k => $v) {
					$i++;
					if($i < $max_name) {
						$content .= ' @:'.$k;
						if($i != ($max_name - 1)) {
							$content .= ',';
						}
					}
					else {
						break;
					}
				}
				$content .= ' và '.(count($belivers)-($max_name-1)).' người khác';
			}
			$content .= ' tin rằng:';
		}
		return $content;
	}
	
	static function getPost($id) {
		return DB::fetch("SELECT * FROM " . T_POST_BELIVE . " WHERE id=". $id." AND status = 1 LIMIT 0,1");
	}
}
?>